package playn.server;

import java.io.InputStream;
import java.util.HashMap;

import javax.servlet.ServletContext;

import tiled.simple.core.Map;
import playn.client.GameService;
import tiled.simple.reader.TMXReader;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

//  unfortunately GWT JavaScript don't support InputStream and with SAX also there are some problems,
//  so I decide to decode tmx on server side and then send it to client via RPC,
//  not so elegant, but works and there are not necessary to rewrite TMXReader under GWT.  

public class GameServiceImpl extends RemoteServiceServlet implements GameService {
	private static final long serialVersionUID = -7118331187186394472L;

	private String[] mapNames={"/level.tmx","/graphics.tmx"};
	
	public HashMap<String,Map> getGameData() throws IllegalArgumentException {
		HashMap<String,Map> maps = new HashMap<String, Map>();
		ServletContext context = getServletContext();
		
		for(int i=0;i<mapNames.length;i++){
			InputStream is = context.getResourceAsStream(mapNames[i]);
			
			TMXReader tmxReader = new TMXReader();
			
			Map map = null;
			try {
				map = tmxReader.readMap(is);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			
			maps.put(mapNames[i], map);
		}
		return(maps);
	}

}
